#!/bin/bash

# svr-admin 前端应用构建脚本 (基于 Tengine)

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 配置变量
IMAGE_NAME="svr-admin"
IMAGE_TAG="latest"
CONTAINER_NAME="svr-admin-container"
PORT="3000"

echo -e "${GREEN}🚀 开始构建 svr-admin 前端应用 (基于 Tengine)${NC}"

# 清理旧的容器和镜像
echo -e "${YELLOW}📦 清理旧的容器和镜像...${NC}"
docker stop $CONTAINER_NAME 2>/dev/null || true
docker rm $CONTAINER_NAME 2>/dev/null || true

# 构建 Docker 镜像
echo -e "${YELLOW}🔨 构建 Docker 镜像...${NC}"
docker build -t $IMAGE_NAME:$IMAGE_TAG .

if [ $? -eq 0 ]; then
    echo -e "${GREEN}✅ Docker 镜像构建成功！${NC}"
else
    echo -e "${RED}❌ Docker 镜像构建失败！${NC}"
    exit 1
fi

# 运行容器
echo -e "${YELLOW}🚀 启动容器...${NC}"
docker run -d \
    --name $CONTAINER_NAME \
    -p $PORT:80 \
    --restart unless-stopped \
    $IMAGE_NAME:$IMAGE_TAG

if [ $? -eq 0 ]; then
    echo -e "${GREEN}✅ 容器启动成功！${NC}"
    echo -e "${GREEN}🌐 应用访问地址: http://localhost:$PORT${NC}"
    echo -e "${GREEN}🏥 健康检查地址: http://localhost:$PORT/health${NC}"
    echo -e "${GREEN}📊 Tengine状态监控: http://localhost:$PORT/tengine_status${NC}"
else
    echo -e "${RED}❌ 容器启动失败！${NC}"
    exit 1
fi

# 显示容器状态
echo -e "${YELLOW}📊 容器状态:${NC}"
docker ps | grep $CONTAINER_NAME

echo -e "${GREEN}🎉 部署完成！${NC}" 